﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CIT.WMS
{
    public partial class FrmCheckUser : Form
    {
        public FrmCheckUser()
        {
            InitializeComponent();
            txt_userID.Focus();
        }
        DataTable DtIn;

        public bool flag = false;
        private void txt_userID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {

                var dt = Bll_CheckUser.GetUserInfo(txt_userID.Text);

                if (dt.Rows.Count == 0)
                {
                    lab_msg.Text = "用户不存在.";
                    lab_msg.ForeColor = Color.Red;
                    txt_userID.Text = "";
                    txt_userID.Focus();
                    return;
                }
                else
                {
                    lab_msg.Text = "";
                }


                dd_CK.Items.Clear();
                DtIn = Bll_CheckUser.GetStorageRegionByUserId(txt_userID.Text);
                if (DtIn.Rows.Count > 0)
                {
                    foreach (DataRow dr in DtIn.Rows)
                    {
                        dd_CK.Items.Add(dr["Region_Name"].ToString());
                    }

                    dd_CK.SelectedIndex = 0;
                }

                Utils.uContext.UserIDk__BackingField = dt.Rows[0]["userid"].ToString();
                Utils.uContext.UserNamek__BackingField = dt.Rows[0]["username"].ToString();
                txt_password.Focus();
            }
        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            flag = false;
            this.Close();
        }


        private void dd_CK_SelectedIndexChanged(object sender, EventArgs e)
        {
            //txt_userID.Focus();

            if (string.IsNullOrEmpty(dd_CK.Text))
            {
                lab_msg.Text = "请选择区域.";
                lab_msg.ForeColor = Color.Red;
                dd_CK.Focus();
                return;
            }
            Utils.uContext.CompanyNamek__BackingField = dd_CK.Text.ToString();

            //flag = true;
            //this.Close();
        }

        private void FrmCheckUser_Load(object sender, EventArgs e)
        {
            //DtIn = Bll_CheckUser.GetStorageRegion();
            //if (DtIn.Rows.Count > 0)
            //{
            //    foreach (DataRow dr in DtIn.Rows)
            //    {
            //        dd_CK.Items.Add(dr["Region_Name"].ToString());
            //    }
            //}

            contect();
        }

        private void dd_CK_KeyPress(object sender, KeyPressEventArgs e)
        {
            //if (e.KeyChar == 13)
            //{
            //    if (string.IsNullOrEmpty(dd_CK.Text))
            //    {
            //        lab_msg.Text = "请选择区域.";
            //        lab_msg.ForeColor = Color.Red;
            //        dd_CK.Focus();
            //        return;
            //    }

            //    Utils.uContext.CompanyNamek__BackingField = dd_CK.Text.ToString();

            //    flag = true;
            //    this.Close();
            //}
        }

        private void btn_login_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(dd_CK.Text))
            {
                lab_msg.Text = "请选择区域.";
                lab_msg.ForeColor = Color.Red;
                dd_CK.Focus();
                return;
            }

            var dt = Bll_CheckUser.CheckUser(txt_userID.Text, (CIT.WMS.Common.PublicUtils.MD5Encrypt(txt_password.Text)));
            if (dt.Rows.Count == 0)
            {
                lab_msg.Text = "密码不正确.";
                lab_msg.ForeColor = Color.Red;
                txt_password.Text = "";
                txt_password.Focus();
                return;
            }

            flag = true;

            FrmMain fm = new FrmMain();
            fm.ShowDialog();
        }


        void contect()
        {
            try
            {
                Utils.NMS.QueryDataTable(Utils.uContext, "select top 10  * from sysdatuser where 1<>1", new CIT.WMS.MESWcf.CmdParameter[0]);
            }
            catch(Exception ex)
            {
                MessageBox.Show("服务器连接失败.");
                this.Close();
            }
        }

    }
}